home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / useful.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  5.5 KB  |  274 lines

  1. //-----------------------------------------------------------------------------
  2. // File: useful.h
  3. //
  4. // Desc: Contains various utility classes and functions to help the
  5. //       UI carry its operations more easily.
  6. //
  7. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  8. //-----------------------------------------------------------------------------
  9.  
  10. #ifndef __USEFUL_H__
  11. #define __USEFUL_H__
  12.  
  13.  
  14. class utilstr
  15. {
  16. public:
  17.     utilstr() : m_str(NULL), m_len(0) {}
  18.     ~utilstr() {Empty();}
  19.  
  20.     operator LPCTSTR () const {return m_str;}
  21.     LPCTSTR Get() const {return m_str;}
  22.  
  23.     const utilstr & operator = (const utilstr &s) {equal(s); return *this;}
  24.     const utilstr & operator = (LPCTSTR s) {equal(s); return *this;}
  25.     const utilstr & operator += (const utilstr &s) {add(s); return *this;}
  26.     const utilstr & operator += (LPCTSTR s) {add(s); return *this;}
  27.  
  28.     LPTSTR Eject();
  29.     void Empty();
  30.     bool IsEmpty() const;
  31.     int GetLength() const;
  32.     void Format(LPCTSTR format, ...);
  33.  
  34. private:
  35.     LPTSTR m_str;
  36.     int m_len;
  37.  
  38.     void equal(LPCTSTR str);
  39.     void add(LPCTSTR str);
  40. };
  41.  
  42. template <class T>
  43. struct rgref {
  44.     rgref(T *p) : pt(p) {}
  45.  
  46.     T &operator [] (int i) {return pt[i];}
  47.     const T &operator [] (int i) const {return pt[i];}
  48.  
  49. private:
  50.     T *pt;
  51. };
  52.  
  53. struct SPOINT {
  54.     SPOINT() :
  55. #define SPOINT_INITIALIZERS \
  56.         p(u.p), \
  57.         s(u.s), \
  58.         a(((int *)(void *)u.a)), \
  59.         x(u.p.x), \
  60.         y(u.p.y), \
  61.         cx(u.s.cx), \
  62.         cy(u.s.cy)
  63.         SPOINT_INITIALIZERS
  64.     {x = y = 0;}
  65.  
  66.     SPOINT(int, POINT *r) :
  67.         p(*r),
  68.         s(*((SIZE *)(void *)r)),
  69.         a(((int *)(void *)r)),
  70.         x(r->x),
  71.         y(r->y),
  72.         cx(r->x),
  73.         cy(r->y)
  74.     {}
  75.  
  76.     SPOINT(const SPOINT &sp) :
  77.         SPOINT_INITIALIZERS
  78.     {p = sp.p;}
  79.  
  80.     SPOINT(int b, int c) :
  81.         SPOINT_INITIALIZERS
  82.     {x = b; y = c;}
  83.  
  84.     SPOINT(const POINT &point) :
  85.         SPOINT_INITIALIZERS
  86.     {p = point;}
  87.  
  88.     SPOINT(const SIZE &size) :
  89.         SPOINT_INITIALIZERS
  90.     {s = size;}
  91.  
  92. #undef SPOINT_INITIALIZERS
  93.  
  94.     SPOINT operator = (const SPOINT &sp) {p = sp.p; return *this;}
  95.     SPOINT operator = (const POINT &_p) {p = _p; return *this;}
  96.     SPOINT operator = (const SIZE &_s) {s = _s; return *this;}
  97.  
  98.     operator POINT () const {return p;}
  99.     operator SIZE () const {return s;}
  100.  
  101.     long &x, &y, &cx, &cy;
  102.     POINT &p;
  103.     SIZE &s;
  104.     rgref<int> a;
  105.  
  106. private:
  107.     union {
  108.         POINT p;
  109.         SIZE s;
  110.         int a[2];
  111.     } u;
  112. };
  113.  
  114. struct SRECT {
  115.     SRECT() :
  116. #define SRECT_INITIALIZERS \
  117.         a(((int *)(void *)u.a)), \
  118.         r(u.r), \
  119.         left(u.r.left), \
  120.         top(u.r.top), \
  121.         right(u.r.right), \
  122.         bottom(u.r.bottom), \
  123.         ul(0, &u.p.ul), \
  124.         lr(0, &u.p.lr)
  125.         SRECT_INITIALIZERS
  126.     {RECT z = {0,0,0,0}; u.r = z;}
  127.     
  128.     SRECT(const SRECT &sr) :
  129.         SRECT_INITIALIZERS
  130.     {u.r = sr.r;}
  131.  
  132.     SRECT(int c, int d, int e, int f) :
  133.         SRECT_INITIALIZERS
  134.     {RECT z = {c,d,e,f}; u.r = z;}
  135.  
  136.     SRECT(const RECT &_r) :
  137.         SRECT_INITIALIZERS
  138.     {u.r = _r;}
  139.  
  140. #undef SRECT_INITIALIZERS
  141.  
  142.     SRECT operator = (const SRECT &sr) {u.r = sr.r; return *this;}
  143.     SRECT operator = (const RECT &_r) {u.r = _r; return *this;}
  144.  
  145.     operator RECT () const {return r;}
  146.  
  147.     long &left, &top, &right, ⊥
  148.     rgref<int> a;
  149.     RECT &r;
  150.     SPOINT ul, lr;
  151.  
  152. private:
  153.     union {
  154.         int a[4];
  155.         RECT r;
  156.         struct {
  157.             POINT ul, lr;
  158.         } p;
  159.     } u;
  160. };
  161.  
  162. int ConvertVal(int x, int a1, int a2, int b1, int b2);
  163. double dConvertVal(double x, double a1, double a2, double b1, double b2);
  164.  
  165. SIZE GetTextSize(LPCTSTR tszText, HFONT hFont = NULL);
  166. int GetTextHeight(HFONT hFont);
  167. SIZE GetRectSize(const RECT &rect);
  168.  
  169. int FormattedMsgBox(HINSTANCE, HWND, UINT, UINT, UINT, ...);
  170. int FormattedErrorBox(HINSTANCE, HWND, UINT, UINT, ...);
  171. int FormattedLastErrorBox(HINSTANCE, HWND, UINT, UINT, DWORD);
  172. BOOL UserConfirm(HINSTANCE, HWND, UINT, UINT, ...);
  173.  
  174. struct AFS_FLAG {DWORD value; LPCTSTR name;};
  175. LPTSTR AllocFlagStr(DWORD value, const AFS_FLAG flag[], DWORD flags);
  176.  
  177. LPTSTR AllocFileNameNoPath(LPTSTR path);
  178.  
  179. LPTSTR AllocLPTSTR(LPCWSTR wstr);
  180. LPTSTR AllocLPTSTR(LPCSTR cstr);
  181. LPWSTR AllocLPWSTR(LPCWSTR wstr);
  182. LPWSTR AllocLPWSTR(LPCSTR str);
  183. LPSTR AllocLPSTR(LPCWSTR wstr);
  184. LPSTR AllocLPSTR(LPCSTR str);
  185. void CopyStr(LPWSTR dest, LPCWSTR src, size_t max);
  186. void CopyStr(LPSTR dest, LPCSTR src, size_t max);
  187. void CopyStr(LPWSTR dest, LPCSTR src, size_t max);
  188. void CopyStr(LPSTR dest, LPCWSTR src, size_t max);
  189.  
  190.  
  191. void PolyLineArrowShadow(HDC hDC, POINT *p, int i);
  192. void PolyLineArrow(HDC hDC, POINT *, int, BOOL bDoShadow = FALSE);
  193.  
  194. BOOL bEq(BOOL a, BOOL b);
  195.  
  196. void DrawArrow(HDC hDC, const RECT &rect, BOOL bVert, BOOL bUpLeft);
  197.  
  198. BOOL ScreenToClient(HWND hWnd, LPRECT rect);
  199. BOOL ClientToScreen(HWND hWnd, LPRECT rect);
  200.  
  201. int StrLen(LPCWSTR s);
  202. int StrLen(LPCSTR s);
  203.  
  204.  
  205. template<class T>
  206. int GetSuperStringByteSize(const T *str)
  207. {
  208.     for (int i = 0;; i++)
  209.         if (!str[i] && !str[i + 1])
  210.             return (i + 2) * sizeof(T);
  211. }
  212.  
  213. template<class T>
  214. T *DupSuperString(const T *str)
  215. {
  216.     int s = GetSuperStringByteSize(str);
  217.     T *ret = (T *)malloc(s);
  218.     if (ret != NULL)
  219.     {
  220.         CopyMemory((void *)ret, (const void *)str, (DWORD)s);
  221.     }
  222.     return ret;
  223. }
  224.  
  225. template<class T>
  226. int CountSubStrings(const T *str)
  227. {
  228.     int n = 0;
  229.  
  230.     while (1)
  231.     {
  232.         str += StrLen(str) + 1;
  233.  
  234.         n++;
  235.  
  236.         if (!*str)
  237.             return n;
  238.     }
  239. }
  240.  
  241. template<class T>
  242. const T *GetSubString(const T *str, int i)
  243. {
  244.     int n = 0;
  245.  
  246.     while (1)
  247.     {
  248.         if (n == i)
  249.             return str;
  250.  
  251.         str += StrLen(str) + 1;
  252.  
  253.         n++;
  254.  
  255.         if (!*str)
  256.             return NULL;
  257.     }
  258. }
  259.  
  260. template<class T>
  261. class SetOnFunctionExit
  262. {
  263. public:
  264.     SetOnFunctionExit(T &var, T value) : m_var(var), m_value(value) {}
  265.     ~SetOnFunctionExit() {m_var = m_value;}
  266.  
  267. private:
  268.     T &m_var;
  269.     T m_value;
  270. };
  271.  
  272.  
  273. #endif //__USEFUL_H__
  274.